package com.forestar.mapControl.mapview.multitouch.listener;

import com.forestar.mapControl.mapview.TestTextureView2;
import com.forestar.mapControl.mapview.multitouch.MoveGestureDetector;
import com.forestar.mapControl.mapview.multitouch.SymbolUtils;
import com.forestar.mapControl.utils.LogD;

public class MoveGestureListener2 implements MoveGestureDetector.OnMoveGestureListener
  {
    private static final int MOVE_THRESHOLD_IN_MM = 0;
      private TestTextureView2 textureView;
    private float lastFocusX;
    private float lastFocusY;
    private boolean scaling;
    private float currentScale;
    private float moveThresholdTolerence;

    private float beginX;
    private float beginY;
    private float endX;
    private float endY;
    private boolean inMoving;

    public MoveGestureListener2(TestTextureView2 textureView)
      {
        this.textureView = textureView;
      }

    @Override
    public boolean onMove(MoveGestureDetector detector)
      {
        try
          {
          } catch (Exception e)
          {
          }
        float focusX = detector.getFocusX();
        float focusY = detector.getFocusY();

        if(null != textureView){
          textureView.move(focusX - lastFocusX, focusY - lastFocusY);
        }
//        this.mapControl.getGestureListenerManager().onMove(detector, this.mapControl);
//        if (mapControl.getCanTouchScrollOrScale())
//          {
//            // mapControl.moveFast(focusX - lastFocusX, focusY - lastFocusY);
//            mapControl.move(focusX - lastFocusX, focusY - lastFocusY);
//          }
        lastFocusX = focusX;
        lastFocusY = focusY;
        return true;
      }

    @Override
    public boolean onMoveBegin(MoveGestureDetector detector)
      {
        try
          {
//            int loadWebTileType = MapzoneConfig.getInstance().getIntValue(LOAD_WEBTILE_TYPE_PREFERENCES_KEY, LOAD_WEBTILE_ONLY_WIFI);

            LogD.d("滑动地图开始");
            inMoving = true;
            lastFocusX = detector.getFocusX();
            lastFocusY = detector.getFocusY();
            currentScale = 1.0f;
            beginX = lastFocusX;
            beginY = lastFocusY;
//            this.mapControl.getGestureListenerManager().onMoveBegin(detector, this.mapControl);
          } catch (Exception e)
          {
          }
        return true;
      }

    @Override
    public void onMoveEnd(MoveGestureDetector detector)
      {
        try
          {
            inMoving = false;
            endX = detector.getFocusX();
            endY = detector.getFocusY();
            float moveX = Math.abs(endX - beginX);
            float moveY = Math.abs(endY - beginY);
//            this.mapControl.getGestureListenerManager().onMoveEnd(detector, this.mapControl);
//            mapControl.getGeoMap().setCleanTileCanvas(true);
            LogD.d("滑动地图结束 开始执行更新缓存并刷新"+getMoveTolerence());
//            if (mapControl.getCanTouchScrollOrScale())
//              {
//                double moveDist = Math.sqrt(moveX * moveX + moveY * moveY);
//                if (moveDist > getMoveTolerence())
//                  {
//                    LogD.d("滑动地图结束 开始执行更新缓存并刷新");
////                    Log.v("MZStabilityTag", "滑动地图结束 开始执行更新缓存并刷新");
////                    mapControl.getGeoMap().refreshAsync();
//                  }
//              }
          } catch (Exception e)
          {
          }

      }

    public float getMoveTolerence()
      {
        if (moveThresholdTolerence == 0)
          {
            moveThresholdTolerence = SymbolUtils.applyDimension(SymbolUtils.COMPLEX_UNIT_MM, MOVE_THRESHOLD_IN_MM);
          }
        return moveThresholdTolerence;
      }

    @Override
    public boolean inMoving()
      {
        return inMoving;
      }
  }
